package com.zz.Personnel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;

public class Jframe extends JFrame {
    private final Container container = getContentPane();
    private Set<Integer> usedIds = new HashSet<>(); // 存储已经使用过的编号
    private JTextField idTextField, nameTextField, salaryTextField, addressTextField; // 分别对应编号、姓名、工资、地址
    private JButton submitButton, cancelButton;

    public Jframe(){
        container.setLayout(null);
        initializeContent();
        setWindowProperties();

        // 为“确定”按钮添加事件监听器
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                validateAndRegister();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hideFields();
            }
        });
    }

    private void hideFields() {
        // 实现隐藏或清空表单内容的功能，例如：
        idTextField.setText("");
        nameTextField.setText("");
        salaryTextField.setText("");
        addressTextField.setText("");
    }

    private void validateAndRegister() {
        String idText = idTextField.getText();
        String nameText = nameTextField.getText();
        String salaryText = salaryTextField.getText();
        String addressText = addressTextField.getText();
        StringBuilder errorMessages = new StringBuilder();

        try {
            int id = Integer.parseInt(idText);
            if (id < 1 || id > 5000 || !usedIds.add(id)) {
                errorMessages.append("编号应在1到5000之间且不能重复！\n");
            }

            if (nameText.length() > 10) {
                errorMessages.append("姓名不能超过10个字符！\n");
            }

            // 这里可以添加对工资和地址的简单验证，并将错误信息添加到errorMessages

            if (errorMessages.length() > 0) {
                JOptionPane.showMessageDialog(null, errorMessages.toString());
                return;
            }

            // 注册成功，清除输入框以便再次输入
            hideFields();

            // 在这里可以进行数据持久化操作，如将信息保存至数据库
            System.out.println("职工信息登记成功：编号=" + id + ", 姓名=" + nameText + ", 工资=" + salaryText + ", 地址=" + addressText);
        } catch (NumberFormatException ex) {
            errorMessages.append("编号必须为数字！");
            JOptionPane.showMessageDialog(null, errorMessages.toString());
        }
    }
    private void setWindowProperties(){
        this.setVisible(true);
        this.setSize(400, 450);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setTitle("职工信息登录界面");
    }

    private void initializeContent(){
        /* 标签 */
        JLabel titleLabel = new JLabel("职工信息登录界面");
        titleLabel.setBounds(100, 30, 400, 50);
        titleLabel.setFont(new Font("楷体", Font.PLAIN, 20));

        JLabel idLabel = new JLabel("编号");
        idLabel.setBounds(60, 70, 100, 50);
        idLabel.setFont(new Font("楷体", Font.PLAIN, 20));

        JLabel nameLabel = new JLabel("姓名");
        nameLabel.setBounds(60, 110, 100, 50);
        nameLabel.setFont(new Font("楷体", Font.PLAIN, 20));

        JLabel salaryLabel = new JLabel("工资");
        salaryLabel.setBounds(60, 150, 100, 50);
        salaryLabel.setFont(new Font("楷体", Font.PLAIN, 20));

        JLabel addressLabel = new JLabel("地址");
        addressLabel.setBounds(60, 190, 100, 50);
        addressLabel.setFont(new Font("楷体", Font.PLAIN, 20));

        /* 文本域 */
        idTextField = new JTextField(20);
        idTextField.setBounds(110, 80, 150, 30);

        nameTextField = new JTextField(20);
        nameTextField.setBounds(110, 120, 150, 30);

        salaryTextField = new JTextField(20);
        salaryTextField.setBounds(110, 160, 150, 30);

        addressTextField = new JTextField(20);
        addressTextField.setBounds(110, 200, 150, 30);

        /* 按钮 */
        cancelButton = new JButton("取消");
        cancelButton.setBounds(80, 250, 80, 40);
        cancelButton.setFont(new Font("楷体", Font.PLAIN, 20));

        submitButton = new JButton("确定");
        submitButton.setBounds(220, 250, 80, 40);
        submitButton.setFont(new Font("楷体", Font.PLAIN, 20));

        container.add(titleLabel);
        container.add(idLabel);
        container.add(nameLabel);
        container.add(salaryLabel);
        container.add(addressLabel);
        container.add(idTextField);
        container.add(nameTextField);
        container.add(salaryTextField);
        container.add(addressTextField);
        container.add(cancelButton);
        container.add(submitButton);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(() -> new Jframe());
    }
}